package com.singerw.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.singerw.mapper.TwUserMapper;
import com.singerw.pojo.TwUser;
import com.singerw.pojo.TwUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static javax.swing.UIManager.get;

/**
 * @version V1.0
 * @Package com.singerw.service
 * @auhter 张欣
 * @date 2020/12/29-8:13 PM
 */

@Service
public class TwUserService {

    @Resource
    private TwUserMapper twUserMapper;
    @Autowired
    private TwUserRoleService twUserRoleService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    public TwUser loadUserByUsername(String UserName) {
        QueryWrapper<TwUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", UserName);
        List<TwUser> twUsers = twUserMapper.selectList(wrapper);
        if (twUsers.size() > 0) {
            return twUsers.get(0);
        }
        return null;
    }

    public List<TwUser> list() {
        return twUserMapper.selectList(null);
    }

    public void save(TwUser twUser) {
        if (twUser.getId() == null) {
            twUser.setPassword(bCryptPasswordEncoder.encode(twUser.getPassword()));
            twUserMapper.insert(twUser);
            String id = loadUserByUsername(twUser.getUsername()).getId();
            TwUserRole twUserRole = new TwUserRole();
            twUserRole.setUserId(id);
            //此处写死角色为「用户」
            twUserRole.setRoleId(2);
            twUserRoleService.save(twUserRole);
            return;
        }
        twUserMapper.updateById(twUser);
    }

    public String getPermissions(String username) {
        TwUser twUser = loadUserByUsername(username);
        JSONArray objects = JSON.parseArray(twUser.getPermissions());
        if (twUser.getSecurePassword() != null) {
            objects.set(objects.size(), "securePwd.existence");
        }
        return objects.toJSONString();
    }

    public void createUser(String userName) {
        if (loadUserByUsername(userName) == null) {
            TwUser twUser = new TwUser();
            twUser.setUsername(userName);
            twUser.setPassword("123456");
            twUser.setPermissions(" [ \"user.all\"]");
            save(twUser);
        }
    }

    public void setPassWord(TwUser twUser, String password) {
        twUser.setPassword(bCryptPasswordEncoder.encode(password));
    }

    public void setPermissions(String userName, String permissions) {
        TwUser twUser = loadUserByUsername(userName);
        JSONArray objects = JSON.parseArray(twUser.getPermissions());
        if (!objects.contains(permissions)) {
            objects.set(objects.size(), permissions);
            twUser.setPermissions(objects.toJSONString());
            save(twUser);
        }

    }

    public void resetPwd(String userName, String passWord) {
        TwUser twUser = loadUserByUsername(userName);
        setPassWord(twUser, passWord);
        save(twUser);
    }

    public boolean checkSecurePassword(String userName, String pwd) {
        TwUser twUser = loadUserByUsername(userName);
        if (twUser.getSecurePassword().equals(pwd)) {
            return true;
        }
        return false;
    }


}
